import isObject from "./is-object";

/**
 * @description 深度合并对象并返回新对象
 * @param {Object} object1 对象一
 * @param {Object} object2 对象二
 * @returns {Object} 合并后的对象
 */
export default function deepMerge(object1, object2) {
	// 检查参数是否为对象
	if (!isObject(object1)) {
		object1 = {};
	}
	if (!isObject(object2)) {
		object2 = {};
	}

	// 合并对象
	const merged = { ...object1 };
	for (const key in object2) {
		if (isObject(object2[key])) {
			merged[key] = deepMerge(merged[key], object2[key]);
		} else {
			merged[key] = object2[key];
		}
	}

	return merged;
}
